package cn.wolfcode.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageBean;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids) {
        log.info("删除id:{}的部门", ids);
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增部门:{}", emp);
        empService.save(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        log.info("根据id:{}查询部门信息", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改部门编号为:{}的名字改为{}", emp.getId(), emp.getName());
        empService.update(emp);
        return Result.success();
    }


    @GetMapping

    public Result list(String name, Integer gender,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") Date end
    ) {
        PageBean pageBean = empService.list(name,gender,page,pageSize,begin,end);
        log.info("查询所有部门信息");
        return Result.success(pageBean);
    }
}

